#!/bin/bash
#
# clandestine.googlecode.com
#

readonly -a ATF=( 0 1 2 "rocket fuel" )
readonly VERSION_FORMAT='%i.%i.%i (%s)'
function Passed () {
	local OUTPUT="$*"
	[ "$OUTPUT" ] && OUTPUT=": $OUTPUT"
	echo "${COLF_GREEN}PASSED${COLR}${OUTPUT}."
}
function Failed () {
	local OUTPUT="$*"
	[ "$OUTPUT" ] && OUTPUT=": $OUTPUT"
	echo ${COLF_RED}FAILED${COLR}${OUTPUT}!
}
function CheckBoolean () {
	local FUNC="$1"
	local TRUE="$2"
	local FALSE="$3"
	echo -n "* Boolean check $1 with TRUE condition $2: "
	if $1 $2; then
		Passed
	else
		Failed
	fi
	echo -n "* Boolean check $1 with FALSE condition $3: "
	if $1 $3; then
		Failed
	else
		Passed
	fi
}
function CheckEqual () {
	declare -a GNUOPT_desc GNUOPT_qfail GNUOPT_mode=( exec )
	GNUOptionsParse "$@"
	eval set -- "$REPLY"
	ArrayPop GNUOPT_mode
	local  LEFT  RIGHT  MODE="$REPLY"  FAIL=""
	REPLY=""
	ArrayPop GNUOPT_desc
	if [ "$REPLY" ]; then
		echo -n "* $REPLY: "
	else
		echo -n "* Comparing '$1' and '$2' in mode $MODE: "
	fi
	case "$MODE" in
		"exec")
			LEFT="$($1)"
			RIGHT="$2"
		;;
		"vars")
			LEFT=$1
			LEFT=${!LEFT}
			RIGHT=$2
			RIGHT=${!RIGHT}
		;;
		"arrays")
			LEFT=`declare -p $1 | sed -n "s|.*='\(.*\)'$|\1|p"`
			RIGHT=`declare -p $2 | sed -n "s|.*='\(.*\)'$|\1|p"`
		;;
		"lists")
			LEFT=$(ArrayValues $1)
			RIGHT=$(ArrayValues $2)
		;;
		*)
			Debug "Unknown mode"
		;;
	esac
	if [ "$LEFT" = "$RIGHT" ]; then
		Passed
	else
		IsEmply GNUOPT_qfail && FAIL="$LEFT"
		Failed "$FAIL"
	fi
}
# Init
echo -n "libclandestine automated check facility is starting... "
	if not source "libclandestine.bash" 2> /dev/null; then
		Failed "can't source the lib"
		exit 1
	fi
	if [ "$(printf "$VERSION_FORMAT" "${LIBCLANDESTINE[@]}")" != "$(printf "$VERSION_FORMAT" "${ATF[@]}")" ]; then
		Failed "version mismatch"
		exit 1
	fi
	declare -- TSTR="wtf?"
	declare -i TINT=666
	declare -a TARR=( [14]="$TSTR" [88]="$TINT" )
	declare -- T64K=$(dd if=/dev/urandom bs=64k count=1 status=noxfer 2> /dev/null)
echo "using version $(printf "$VERSION_FORMAT" "${LIBCLANDESTINE[@]}")."
echo "--- error test runs"
Debug "${COLF_GREEN}A TEST DEBUG MESSAGE${COLR}"
Warn "${COLF_GREEN}A TEST WARNING MESSAGE${COLR}"
echo "--- function functions series"
CheckBoolean FunctionLookup CheckBoolean NonExistant
echo "--- variable types series"
CheckEqual "TypeOf TSTR" "-"
CheckEqual "TypeOf TINT" "i"
CheckEqual "TypeOf TARR" "a"
CheckBoolean IsSet TSTR TSTR2
CheckBoolean IsString TSTR TINT
CheckBoolean IsInteger TINT TARR
CheckBoolean IsArray TARR TSTR
echo "--- variable sizes series"
CheckEqual "Length TSTR" "4"
CheckEqual "Length TINT" "1"
CheckEqual "Length TARR" "2"
echo "--- general array series"
CheckEqual "ArrayKeys TARR" "14 88"
eval TARR2=( $(ArrayValues TARR) )
CheckEqual --mode=lists "TARR" "TARR2"
CheckEqual "ArrayImplode TARR ++" "$TSTR++$TINT"
ArrayPush TARR2 "$T64K"
ArrayPop TARR2
T64K2="$REPLY"
CheckEqual --mode=vars --qfail "T64K2" "T64K" --desc="ArrayPop 64k value check"
CheckEqual --mode=lists "TARR" "TARR2" --desc="ArrayPop 64k array check"
echo "--- empty array series"
declare -a TARR=( )
CheckEqual "ArrayKeys TARR" ''
CheckEqual "ArrayValues TARR" ''
CheckEqual "ArrayImplode TARR ++" ''
CheckEqual "ArrayFirstKey TARR" ''
CheckEqual "ArrayLastKey TARR" ''
CheckEqual "ArrayPop TARR" ''
CheckEqual "ArrayShift TARR" ''
CheckEqual "ArrayReverse TARR" ''
